package com.shine.minepressure.view.pop;

import android.app.DatePickerDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.util.XPopupUtils;
import com.shine.minepressure.R;
import com.shine.minepressure.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class History4DataPopup extends BottomPopupView implements View.OnClickListener {
    OnConfirmListener confirmListener;
    OnCancelListener cancelListener;


    private static List<String> mAreaMessage = new ArrayList<>();
    private List<String> mAreaSensorMessage = new ArrayList<>();
    private static List<String> mStationMessage = new ArrayList<>();
    private String mAreaName, mSensorName, mStationName;
    private int mAreaPosition, mAreaSensorPosition, mStationPosition;
    private String mTimeStart, mTimeEnd;

    private TextView areaMessageText, areaSensorMessageText, sensorMessageText;
    private TextView timeStartText, timeEndText;

    public interface OnPopClickListener {
        void getSensorMessage(Integer mAreaPosition);

        void onConfirmClick(String areaName, String sensorName, String stationName,
                            String beginTime, String endTime,
                            int areaPosition, int areaSensorPosition, int stationPosition);
    }

    private OnPopClickListener onPopClickListener;

    public History4DataPopup(@NonNull Context context, List<String> areaMessage, List<String> areaSensorMessage, List<String> stationMessage,
                             String areaName, String sensorName, String stationName,
                             String beginTime, String endTime,
                             int areaPosition, int areaSensorPosition, int stationPosition) {
        super(context);
        mAreaMessage.addAll(areaMessage);
        mAreaSensorMessage.addAll(areaSensorMessage);
        mStationMessage.addAll(stationMessage);

        mAreaName = areaName;
        mSensorName = sensorName;
        mStationName = stationName;

        mTimeStart = beginTime;
        mTimeEnd = endTime;

        mAreaPosition = areaPosition;
        mAreaSensorPosition = areaSensorPosition;
        mStationPosition = stationPosition;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_history4_data;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        ImageView cancel = (ImageView) findViewById(R.id.cancel);
        TextView confirm = (TextView) findViewById(R.id.confirm);

        areaMessageText = (TextView) findViewById(R.id.areaMessageText);
        areaSensorMessageText = (TextView) findViewById(R.id.areaSensorMessageText);
        sensorMessageText = (TextView) findViewById(R.id.sensorMessageText);
        timeStartText = (TextView) findViewById(R.id.timeStart);
        timeEndText = (TextView) findViewById(R.id.timeEnd);

        if (!TextUtils.isEmpty(mAreaName))
            areaMessageText.setText(mAreaName);
        if (!TextUtils.isEmpty(mSensorName))
            areaSensorMessageText.setText(mSensorName);

        sensorMessageText.setText(mStationMessage.get(mStationPosition));

        if (!TextUtils.isEmpty(mTimeStart))
            timeStartText.setText(mTimeStart);
        if (!TextUtils.isEmpty(mTimeEnd))
            timeEndText.setText(mTimeEnd);

        confirm.setOnClickListener(this);
        cancel.setOnClickListener(this);
        timeStartText.setOnClickListener(this);
        timeEndText.setOnClickListener(this);

        findViewById(R.id.areaMessageLayout).setOnClickListener(this);
        findViewById(R.id.areaSensorMessageLayout).setOnClickListener(this);
        findViewById(R.id.sensorMessageLayout).setOnClickListener(this);

    }

    public History4DataPopup setListener(OnConfirmListener confirmListener, OnCancelListener onCancelListener, OnPopClickListener onPopClickListener) {
        this.confirmListener = confirmListener;
        this.cancelListener = onCancelListener;
        this.onPopClickListener = onPopClickListener;
        return this;
    }

    private void setAreaMessage(List<String> areaMessageList) {
        CommonListBottomPopup commonListBottomPopup = new CommonListBottomPopup(getContext(), areaMessageList);
        new XPopup.Builder(getContext())
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .isDestroyOnDismiss(true)
                .maxHeight(200)
                .asCustom(commonListBottomPopup)/*.enableDrag(false)*/
                .show();
        commonListBottomPopup.setListener(new CommonListBottomPopup.OnPopItemClickListener() {
            @Override
            public void onPopItemClick(View view, int position) {
                mAreaPosition = position;
                mAreaName = areaMessageList.get(position);
                areaMessageText.setText(mAreaName);
                commonListBottomPopup.dismiss();
            }
        });
    }

    public void setAreaSensorMessage(List<String> areaSensorMessageList) {
        CommonListBottomPopup commonListBottomPopup2 = new CommonListBottomPopup(getContext(), areaSensorMessageList);
        new XPopup.Builder(getContext())
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .isDestroyOnDismiss(true)
                .maxHeight(200)
                .asCustom(commonListBottomPopup2)/*.enableDrag(false)*/
                .show();
        commonListBottomPopup2.setListener(new CommonListBottomPopup.OnPopItemClickListener() {
            @Override
            public void onPopItemClick(View view, int position) {
                mAreaSensorPosition = position;
                mSensorName = areaSensorMessageList.get(position);
                areaSensorMessageText.setText(mSensorName);
                commonListBottomPopup2.dismiss();
            }
        });
    }

    private void setStationMessage(List<String> stationMessageList) {
        CommonListBottomPopup commonListBottomPopup1 = new CommonListBottomPopup(getContext(), stationMessageList);
        new XPopup.Builder(getContext())
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .isDestroyOnDismiss(true)
                .maxHeight(200)
                .asCustom(commonListBottomPopup1)/*.enableDrag(false)*/
                .show();
        commonListBottomPopup1.setListener(new CommonListBottomPopup.OnPopItemClickListener() {
            @Override
            public void onPopItemClick(View view, int position) {
                mStationPosition = position;
                mStationName = stationMessageList.get(position);
                sensorMessageText.setText(mStationName);
                commonListBottomPopup1.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm:
                if (confirmListener != null) confirmListener.onConfirm();

                if (areaMessageText.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请选择测区列表");
                    return;
                }
                if (areaSensorMessageText.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请选择传感器信息");
                    return;
                }
                if (sensorMessageText.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请选择分站信息");
                    return;
                }
                if (timeEndText.getText().toString().equals("开始时间") || timeStartText.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请选择开始时间");
                    return;
                }
                if (timeEndText.getText().toString().equals("结束时间") || timeEndText.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请选择结束时间");
                    return;
                }
                onPopClickListener.onConfirmClick(mAreaName, mSensorName, mStationName,
                        mTimeStart, mTimeEnd,
                        mAreaPosition, mAreaSensorPosition, mStationPosition);
                dismiss();
                break;
            case R.id.cancel:
                if (cancelListener != null) cancelListener.onCancel();
                dismiss();
                break;
            case R.id.timeStart:
                Calendar calendar = Calendar.getInstance();
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        timeStartText.setText(year + "/" + (month + 1) + "/" + dayOfMonth + " " + "00:00:00");
//                        new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
//                            @Override
//                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//                                timeStart.setText(year + "-" + (month + 1) + "-" + dayOfMonth + " " + hourOfDay + ":" + minute);
//                            }
//                        }, 0, 0, true).show();

                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH))
                        .show();
                break;
            case R.id.timeEnd:
                Calendar calendar3 = Calendar.getInstance();
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        timeEndText.setText(year + "/" + (month + 1) + "/" + dayOfMonth + " " + "23:59:59");
//                        new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
//                            @Override
//                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//                                timeStart.setText(year + "-" + (month + 1) + "-" + dayOfMonth + " " + hourOfDay + ":" + minute);
//                            }
//                        }, 0, 0, true).show();

                    }
                }, calendar3.get(Calendar.YEAR), calendar3.get(Calendar.MONTH), calendar3.get(Calendar.DAY_OF_MONTH))
                        .show();
                break;
            case R.id.areaMessageLayout:
                setAreaMessage(mAreaMessage);
                break;
            case R.id.areaSensorMessageLayout:
                onPopClickListener.getSensorMessage(mAreaPosition);
                break;
            case R.id.sensorMessageLayout:
                setStationMessage(mStationMessage);
                break;
            default:
                break;
        }
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
    }

    @Override
    protected int getMaxHeight() {
//        return XPopupUtils.getWindowHeight(getContext());
        return (int) (XPopupUtils.getWindowHeight(getContext()) * .9f);
    }

}
